﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Add_products_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] != null)
        {
            btnRedirect.Visible = true;
            BtnLogin.Visible = false;
            BtnLogout.Visible = true;
        }
        else
        {
            btnRedirect.Visible = false;
            BtnLogin.Visible = true;
            BtnLogout.Visible = false;
        }

        if (!IsPostBack)
        {
            if (Session["Username"] == null)
            {
                Response.Redirect("../Login/Login.aspx");
            }
        }
        
    }
    private bool isAllFieldRequire()
    {
        if (string.IsNullOrWhiteSpace(txtPid.Text) || string.IsNullOrWhiteSpace(txtName.Text)
            || string.IsNullOrWhiteSpace(txtPrice.Text) || string.IsNullOrWhiteSpace(lblStocked.Text))
        {
            return false;
        }
        return true;
    }
    private bool isRightPID()
    {
        String strCon = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ToString();
        SqlConnection myCon = new SqlConnection(strCon);
        myCon.Open();
        string sql = "select * from _Product where PID = '{0}'";
        sql = string.Format(sql, txtPid.Text);
        SqlCommand cmd = new SqlCommand(sql, myCon);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable ds = new DataTable();
        da.Fill(ds);
        if (ds.Rows.Count == 0)
        {
            myCon.Close();
            return true;
        }
        myCon.Close();
        return false;
    }

    private bool isRightPrice() 
    {
        try
        {
            float.Parse(txtPrice.Text);
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }
    private bool isRightStocked() 
    {
        try
        {
            int.Parse(txtStoked.Text);
        }
        catch (Exception)
        {
            return false;
        }
        return true;
    }
    
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (!isAllFieldRequire())
        {
            Label8.Text = "All field require";
            return;
        }
        if (!isRightPID())
        {
            Label8.Text = "Duplicated PID";
            return;
        }
        if (!isRightStocked()) 
        {
            Label8.Text = "Stocked need to be integer";
            return;
        }
        if (!isRightPrice()) 
        {
            Label8.Text = "Price need to be float";
            return;
        }

        int length = FileUpload1.PostedFile.ContentLength;
        byte[] imageData = new byte[length];
        FileUpload1.PostedFile.InputStream.Read(imageData, 0, length);
        String strCon = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ToString();
        SqlConnection myCon = new SqlConnection(strCon);
        myCon.Open();
        SqlCommand cmd = new SqlCommand("InsertProduct", myCon);
        cmd.CommandType = CommandType.StoredProcedure;
       
        cmd.Parameters.AddWithValue("@PID", txtPid.Text);
        cmd.Parameters["@PID"].Direction = ParameterDirection.Input;
        cmd.Parameters.AddWithValue("@Name", txtName.Text);
        cmd.Parameters["@Name"].Direction = ParameterDirection.Input;
        cmd.Parameters.AddWithValue("@Price", txtPrice.Text);
        cmd.Parameters["@Price"].Direction = ParameterDirection.Input;
        cmd.Parameters.Add("@Image", SqlDbType.Image);
        cmd.Parameters["@Image"].Direction = ParameterDirection.Input;
        cmd.Parameters["@Image"].Value = imageData;
        cmd.Parameters.AddWithValue("@Stocked", txtStoked.Text);
        cmd.Parameters["@Stocked"].Direction = ParameterDirection.Input;
        cmd.Parameters.AddWithValue("@Description", txtDes.Text);
        cmd.Parameters["@Description"].Direction = ParameterDirection.Input;
        

        try
        {
            cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
        }
        txtDes.Text = "";
        txtName.Text = "";
        txtPid.Text = "";
        txtPrice.Text = "";
        txtStoked.Text = "";
        myCon.Close();
        Response.Redirect("AddProducts.aspx");

    }

    protected void tbProduct_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        String strCon = ConfigurationManager.ConnectionStrings["SQLConnectionString"].ToString();
        SqlConnection myCon = new SqlConnection(strCon);
        myCon.Open();
        string sql = "delete from _User where PID = '{0}'";
        sql = string.Format(sql, tbProduct.Rows[e.RowIndex].Cells[1].Text);
        SqlCommand cmd = new SqlCommand(sql, myCon);
        try
        {
            cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
        }
        myCon.Close();
    }
    protected void tbProduct_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        txtPid.Text = tbProduct.Rows[e.NewSelectedIndex].Cells[1].Text;
        txtName.Text = tbProduct.Rows[e.NewSelectedIndex].Cells[2].Text;
        txtPrice.Text = tbProduct.Rows[e.NewSelectedIndex].Cells[3].Text;
        txtStoked.Text = tbProduct.Rows[e.NewSelectedIndex].Cells[5].Text;
        txtDes.Text = tbProduct.Rows[e.NewSelectedIndex].Cells[6].Text;
        
    }
    protected void btnRedirect_Click(object sender, EventArgs e)
    {
        clsLogIn a = (clsLogIn)(Session["Username"]);

        if (a.IsAdmin)
        {
            Response.Redirect("../User Information/Information.aspx");
        }
        else
        {
            Response.Redirect("../Add Products/AddProducts.aspx");
        }
    }
    protected void BtnLogin_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Login/Login.aspx");

    }
    protected void BtnLogout_Click1(object sender, EventArgs e)
    {
        Session["Username"] = null;
        Response.Redirect("../Login/Login.aspx");
    }
    protected void BtnOrder_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Order/Default.aspx");
    }
}


